<template>
  <div class="login">
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
       <van-button class="resigt" round block type="info" @click="goregister">没有账号？点击注册</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import {setToken} from "../../util/auth"
import {gologin} from "../../api/user"
import { Toast } from "vant";
export default {
  data() {
    return {
      username: "",
      password: "", 
    };
  },
  computed: {},
  watch: {},

  methods: {
    //跳转到注册页面
    goregister(){
     this.$router.push("/register")
    },
      async onSubmit(values) {
      console.log('submit', values);
      const result= await gologin(values);
      console.log(result);
      if(result.code==="success"){
          setToken(result.token);
           localStorage.setItem("token", result.token);
        this.$store.commit("setToken", result.token); // 将token存入vuex
          console.log(result.token);
            Toast("登录成功");
          this.$router.push("/");
      }
    },
  },
  created() {},
  mounted() {},
  components: {},
};
</script>
<style scoped>
.resigt{
  margin-top:10px ;
}
</style>
